home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 23 web forms and controls / databinding / repeaterform.aspx.vb < prev    next >
Encoding:
Text File  |  2002-03-17  |  3.4 KB  |  86 lines

  1. Imports System.Data.OleDb
  2.  
  3. Public Class RepeaterForm
  4.     Inherits System.Web.UI.Page
  5.  
  6.     Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater
  7.     Protected WithEvents Repeater2 As System.Web.UI.WebControls.Repeater
  8.     Protected WithEvents Label1 As System.Web.UI.WebControls.Label
  9.  
  10. #Region " Web Form Designer Generated Code "
  11.  
  12.     'This call is required by the Web Form Designer.
  13.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  14.  
  15.     End Sub
  16.  
  17.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  18.         'CODEGEN: This method call is required by the Web Form Designer
  19.         'Do not modify it using the code editor.
  20.         InitializeComponent()
  21.     End Sub
  22.  
  23. #End Region
  24.  
  25.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  26.         'Put user code to initialize the page here
  27.         If Not Page.IsPostBack Then
  28.             Dim cn As New OleDbConnection(BiblioConnString)
  29.             Dim cmd As New OleDbCommand("SELECT * FROM Publishers", cn)
  30.             cn.Open()
  31.             Dim dr As OleDbDataReader = cmd.ExecuteReader()
  32.             Repeater1.DataSource = dr
  33.             Repeater1.DataBind()
  34.             dr.Close()
  35.  
  36.             ' Read the data and bind it to the control.
  37.             dr = cmd.ExecuteReader()
  38.             Repeater2.DataSource = dr
  39.             Repeater2.DataBind()
  40.             dr.Close()
  41.             cn.Close()
  42.         End If
  43.     End Sub
  44.  
  45.     ' This event fires when each itam of the Repeater control is bound
  46.  
  47.     Private Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
  48.         Select Case e.Item.ItemType
  49.             Case ListItemType.Item, ListItemType.AlternatingItem
  50.                 Dim dbr As System.Data.Common.DbDataRecord = DirectCast(e.Item.DataItem, System.Data.Common.DbDataRecord)
  51.                 Dim city As String = dbr("city").ToString
  52.                 If city.Length > 0 Then
  53.                     Dim lc As New Literal()
  54.                     lc.Text = " - " & city
  55.                     e.Item.Controls.Add(lc)
  56.                 End If
  57.         End Select
  58.     End Sub
  59.  
  60.     ' this code demonstrates how you can trap the ItemCreated event
  61.  
  62.     Dim itemCount As Integer
  63.  
  64.     Private Sub Repeater1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemCreated
  65.         Select Case e.Item.ItemType
  66.             Case ListItemType.Item, ListItemType.AlternatingItem
  67.                 itemCount += 1
  68.             Case ListItemType.Footer
  69.                 Dim lc As New Literal()
  70.                 lc.Text = itemCount & " publishers."
  71.                 e.Item.Controls.Add(lc)
  72.         End Select
  73.     End Sub
  74.  
  75.     ' this event fires when a button in the second Repeater is clicked
  76.  
  77.     Private Sub Repeater2_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles Repeater2.ItemCommand
  78.         Select Case e.CommandName
  79.             Case "moreinfo"
  80.                 ' show additional info on the selected publisher
  81.                 Dim pubId As String = e.CommandArgument.ToString
  82.                 Label1.Text = "User requested information on publisher with PubId=" & pubId
  83.         End Select
  84.     End Sub
  85. End Class
  86.